package com.unicorn.util;

import com.fasterxml.jackson.core.Base64Variant;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.json.JsonGeneratorImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectSerializer;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * Created by windy on 16-11-3.
 */
@Component
public class UnicornHttpRequestHandler<T> {

    private static Logger logger= LoggerFactory.getLogger(UnicornHttpRequestHandler.class);

    @Autowired
    protected JsonResponse<T> jsonResponse;

    @Autowired
    protected ObjectMapper objectMapper;






     public JsonResponse handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,Logger logger) throws ServletException, IOException {

         logger.info(new Date()+" "+httpServletRequest.getRemoteAddr()+" request:"+httpServletRequest.getRequestURI()+" with 【sessionId】 "+httpServletRequest.getRequestedSessionId());
         if(httpServletResponse.getStatus()==HttpServletResponse.SC_OK){
             jsonResponse.code=JsonResponse.STATUS_OK;
             jsonResponse.message=JsonResponse.MSG_SUCCESS;
         }else if(httpServletResponse.getStatus()==HttpServletResponse.SC_BAD_REQUEST ||httpServletResponse.getStatus()==HttpServletResponse.SC_BAD_GATEWAY||httpServletResponse.getStatus()==HttpServletResponse.SC_FORBIDDEN){
             jsonResponse.code=JsonResponse.STATUS_BAD_REQUEST;
             jsonResponse.message=JsonResponse.MSG_BAD_REQUEST;
         }else{
             jsonResponse.code=JsonResponse.STATUS_FAIL;
             jsonResponse.message=JsonResponse.MSG_FAIL;
         }

         return jsonResponse;


    }

    public JsonResponse handleRequest(T data,HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,Logger logger) throws ServletException, IOException {

        jsonResponse.data=data;
        logger.info(new Date()+" "+httpServletRequest.getRemoteAddr()+" request:"+httpServletRequest.getRequestURI()+" with 【sessionId】 "+httpServletRequest.getRequestedSessionId());
        if(httpServletResponse.getStatus()==HttpServletResponse.SC_OK){
            jsonResponse.code=JsonResponse.STATUS_OK;
            jsonResponse.message=JsonResponse.MSG_SUCCESS;
        }else if(httpServletResponse.getStatus()==HttpServletResponse.SC_BAD_REQUEST ||httpServletResponse.getStatus()==HttpServletResponse.SC_BAD_GATEWAY||httpServletResponse.getStatus()==HttpServletResponse.SC_FORBIDDEN){
            jsonResponse.code=JsonResponse.STATUS_BAD_REQUEST;
            jsonResponse.message=JsonResponse.MSG_BAD_REQUEST;
        }else{
            jsonResponse.code=JsonResponse.STATUS_FAIL;
            jsonResponse.message=JsonResponse.MSG_FAIL;
        }

        return jsonResponse;


    }


    public void warning(String msgDeleteSuccess, Logger logger) {

        jsonResponse.code=JsonResponse.STATUS_FAIL;
        jsonResponse.message=msgDeleteSuccess;
        logger.warn(new Date()+this.getClass().getName()+ jsonResponse.code+ jsonResponse.message);

    }
}
